$(document).ready(function(){

	// 调整主体宽度
	setContentWidth();
	$(window).resize(setContentWidth);

	tab();
});


String.prototype.utf8Length=function () {
	var totalLength = 0;
	var i;
	var charCode;
	for (i = 0; i < this.length; i++) {
		charCode = this.charCodeAt(i);
		if (charCode < 0x007f) {
			totalLength = totalLength + 1;
		} else if ((0x0080 <= charCode) && (charCode <= 0x07ff)) {
			totalLength += 2;
		} else if ((0x0800 <= charCode) && (charCode <= 0xffff)) {
			totalLength += 3;
		}
	}
	return totalLength;
}

/*
	日期格式化
	格式 YYYY/yyyy/YY/yy 表示年份
	MM/M 月份
	W/w 星期
	dd/DD/d/D 日期
	hh/HH/h/H 时间
	mm/m 分钟
	ss/SS/s/S 秒
*/
Date.prototype.Format = function(formatStr){
	var str = formatStr;
	var Week = ['日','一','二','三','四','五','六'];

	str=str.replace(/yyyy|YYYY/,this.getFullYear());
	str=str.replace(/yy|YY/,(this.getYear() % 100)>9?(this.getYear() % 100).toString():'0' + (this.getYear() % 100));

	str=str.replace(/MM/,this.getMonth()>9?this.getMonth().toString():'0' + this.getMonth());
	str=str.replace(/M/g,this.getMonth());

	str=str.replace(/w|W/g,Week[this.getDay()]);

	str=str.replace(/dd|DD/,this.getDate()>9?this.getDate().toString():'0' + this.getDate());
	str=str.replace(/d|D/g,this.getDate());

	str=str.replace(/hh|HH/,this.getHours()>9?this.getHours().toString():'0' + this.getHours());
	str=str.replace(/h|H/g,this.getHours());

	str=str.replace(/mm/,this.getMinutes()>9?this.getMinutes().toString():'0' + this.getMinutes());
	str=str.replace(/m/g,this.getMinutes());

	str=str.replace(/ss|SS/,this.getSeconds()>9?this.getSeconds().toString():'0' + this.getSeconds());
	str=str.replace(/s|S/g,this.getSeconds());

	return str;
}

//---------------------------------------------------
// 判断闰年
//---------------------------------------------------

Date.prototype.isLeapYear = function()
{
	return ((0 === this.getYear() % 4) && ((this.getYear() % 100 !== 0) || (this.getYear() % 400 !== 0)));
}

// 调整主体宽度
function setContentWidth() {
	if (!$("#mainAside")) {
		return;
	}

	if ($("#mainAside").width() === 0) {
		$("#mainContent_content").css("width","100%");
		return;
	}

	$("#mainContent_content").css("width",($("#mainContent").width() - 40 - $("#mainAside").width()) + "px");
}


// tab选项卡功能
function tab() {
	$("body").on("click",".tabNav li",function(){
		var index = $(".tabNav li").index($(this));
		$(".tabNav .active").removeClass("active");
		$(".tabContent .active").removeClass("active");
		$(this).addClass("active");
		$(".tabContent > div:nth-of-type("+(index+1)+")").addClass("active");
	});
}


var Prompt = {
		alert : function(title,prompt,func){
			$("#prompt_alert_title").html(title);
			$("#prompt_alert_text").html(prompt);
			$("#prompt_confirm_yes").unbind();
			$("#prompt_alert_yes").on("click",function(){
				if(func)func();
				$('#prompt_alert').modal('hide');
			});
			$('#prompt_alert').modal('show');
		},

		confirm : function(title,prompt,func,func2){
			$("#prompt_confirm_title").html(title);
			$("#prompt_confirm_text").html(prompt);
			$("#prompt_confirm_yes").unbind();
			$("#prompt_confirm_yes").on("click",function(){
				if(func)func();
				$('#prompt_confirm').modal('hide');
			});
			$("#prompt_confirm_no").on("click",function(){
				if(func2)func2();
				$('#prompt_confirm').modal('hide');
			});
			$('#prompt_confirm').modal('show');
		},
		
		refresh : function(){
			location.href=location.href;
		}
}

Date.prototype.format = function(format) {  
    var date = {  
           "M+": this.getMonth() + 1,  
           "d+": this.getDate(),  
           "H+": this.getHours(),  
           "m+": this.getMinutes(),  
           "s+": this.getSeconds(),  
           "q+": Math.floor((this.getMonth() + 3) / 3),  
           "S+": this.getMilliseconds()  
    };  
    if (/(y+)/i.test(format)) {  
           format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));  
    }  
    for (var k in date) {  
           if (new RegExp("(" + k + ")").test(format)) {  
                  format = format.replace(RegExp.$1, RegExp.$1.length == 1  
                         ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));  
           }  
    }  
    console.log(format);
    return format;  
} 

function limitInputNumber(obj){
	obj.value=obj.value.replace(/\D/g,'');
}

function limitPageNumber(obj,max){
	obj.value=obj.value.replace(/\D/g,'');
	if(obj.value.trim()=="") obj.value=1;
	if(obj.value<1) obj.value=1;
	if(obj.value>max) obj.value=max;
}
function limitPageNumber2(obj,constant){
	obj.value=obj.value.replace(/\D/g,'');
	if(obj.value.trim()=="") obj.value=constant;
	if(obj.value<1) obj.value=constant;
}
function pageSizeChange(obj){
	var url=location.href;
	url=setUrlParam(url,'pageSize',$(obj).val())
	location.href=url;
}

function setUrlParam(url,param,value){
	var newUrl = "";
	var reg = new RegExp("(^|)" + param + "=([^&]*)(|$)");
	var tmp = param + "=" + value;
	if (url.match(reg) != null) {
		newUrl = url.replace(eval(reg), tmp);
	} else {
		if (url.match("[\?]")) {
			newUrl = url + "&" + tmp;
		} else {
			newUrl = url + "?" + tmp;
		}
	}
	return newUrl; 
}

function validateString(name,tag,length,nullAble,isAlert){
	var str=$(tag).val().trim();
	$(tag).val(str);
	if(!nullAble && str==""){
		Prompt.alert("提示",name+"不能为空");
		return false;
//	}else if(str.utf8Length()>length){
	}else if(str.length>length){
		Prompt.alert("提示",name+"太长");
		return false;
	}
	return true;
}

function validateRegex(name,tag,regex,nullAble,isAlert){
	var value=$(tag).val().trim();
	$(tag).val(value);
	if(!nullAble && value==""){
		Prompt.alert("提示",name+"不能为空");
		return false;
	}else if(!new RegExp(regex).test(value)){
		Prompt.alert("提示",name+"格式错误");
		return false;
	}
	return true;
}

function validateNumber(name,tag,min,max,nullAble,isAlert){
	var value=$(tag).val();
	if(!nullAble && value==""){
		Prompt.alert("提示",name+"不能为空");
		return false;
	}else if(nullAble && value==""){
		Prompt.alert("提示",name+"不能小于"+min);
		return false;
	}else if(value<min){
		Prompt.alert("提示",name+"不能小于"+min);
		return false;
	}else if(value>max){
		Prompt.alert("提示",name+"不能大于"+max);
		return false;
	}
	return true;
}

function validateSelect(name,tag,nullAble,isAlert){
	var value=$(tag).val();
	if(!nullAble){
		if(!value || value==""){
			Prompt.alert("提示","请选择"+name);
			return false;
		}
	}
	return true;
}